﻿#ifndef DRAWFORM_H
#define DRAWFORM_H

/*
* @author   Puuck
* @date     2020-09-01
*/

#include <QWidget>

namespace Ui
{
    class DrawForm;
}

class DrawForm : public QWidget
{
    Q_OBJECT

public:
    enum DrawMode
    {
        DRAW_NULL = 0,
        DRAW_LINE,
        DRAW_POLYGON,
        DRAW_RECT,
    };

    struct DrawLine
    {
        QLine line;
        bool selected;
    };

    struct DrawPolygon
    {
        QPolygon polygon;
        bool selected;
    };

    struct DrawRect
    {
        QRect rect;
        bool selected;
        QString desc;
        bool right;//是否是右键画得
    };

public:
    explicit DrawForm(QWidget *parent = nullptr);
    ~DrawForm();

    void setDrawMode(int drawMode);

    void clearLines();
    void clearPolygons();
    void clearRects();
    void clearImage();
    void clear();

    void setImage(QByteArray imageArr);

    QPoint pointF2Point(QPointF pointF);
    QPointF point2PointF(QPoint point);
    QRect rectF2Rect(QRectF rectF);
    QRectF rect2RectF(QRect rect);

    QList<DrawForm::DrawLine> lines() const;
    void setLines(const QList<DrawLine> &lines);

    QList<DrawForm::DrawPolygon> polygons() const;
    void setPolygons(const QList<DrawPolygon> &polygons);

    QList<DrawForm::DrawRect> rects() const;
    void setRects(const QList<DrawRect> &rects);

    void set_detect_Rects(const QList<DrawRect> &rects);
    void set_detect_Lines(const QList<DrawLine> &lines);
    void set_detect_Polygons(const QList<DrawPolygon> &polygons);

    void set_compare_rect(const DrawRect &rect);

protected:
    virtual void paintEvent(QPaintEvent *event);
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event) ;

private:
    void paintLine(const DrawLine &line);
    void paintAlarmLine(const DrawLine &line);

    void paintPolygon(const DrawPolygon &polygon);
    void paintAlarmPolygon(const DrawPolygon &polygon);

    void paintRect(const DrawRect &rect, QColor color = QColor(255, 255, 0, 255));
    void paintAlarmRect(const DrawRect &rect);


    bool isInLine(const QPoint &p, const QLine &line);
    double p2pLen(const QPoint &p1, const QPoint &p2);
    bool isInPolygon(const QPoint &p, const QPolygon &polygon);

private:
    Ui::DrawForm *ui;

    QColor m_ptColor;    // 点颜色
    QColor m_alarmColor; //报警框颜色
    QColor m_detectColor; //检测框颜色
    int m_ptRadius;      // 点半径

    QColor m_lineColor;    // 线颜色
    int m_lineWidth;       // 线宽

    int m_drawMode;    // 绘图模式

    DrawLine m_line;            // 线
    QList<DrawLine> m_lines;    // 线集合
    int m_selectedLineId;       // 选中线段序号
    QLine m_selectedLine;       // 选中线段

    QPoint m_lastPressPt;    // 鼠标最后按下点
    int m_selectedPtId;      // 选中点序号

    DrawPolygon m_polygon;            // 多边形
    QList<DrawPolygon> m_polygons;    // 多边形集合
    int m_selectedPolygonId;          // 选中多边形序号
    QPolygon m_selectedPolygon;       // 选中多边形

    DrawRect m_rect;            // 矩形
    QList<DrawRect> m_rects;    // 矩形集合

    //检测框
    QList<DrawRect> m_detect_rects;    // 大块检测矩形集合
    QList<DrawPolygon> m_detect_polygons;

    QList<DrawLine> m_detect_lines;

    DrawRect compare_rect;//对比框

    int m_selectedRectId;       // 选中矩形序号
    QRect m_selectedRect;       // 选中矩形

    QImage image;

    std::string m_image;    // 分析图片
};

#endif    // DRAWFORM_H
